package com.xszx.knowledge.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xszx.knowledge.dto.PageReq;
import com.xszx.knowledge.entity.Ebook;
import com.xszx.knowledge.mapper.EbookMapper;
import com.xszx.knowledge.service.EbookService;
import com.xszx.knowledge.util.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 电子书 前端控制器
 * </p>
 *
 * @author wencai
 * @since 2023/04/20 10:48
 */
@Api(tags = "电子书管理")
@RestController
@RequestMapping("/ebook")
public class EbookController {
    @Resource
    private EbookService ebookService;

    /**
     * 根据具体参数查询全部的数据
     * @return
     */
    @GetMapping("/getAll")
    public Result getAllEbook(Long category2Id){
        category2Id = (category2Id == 0)?null:category2Id;
        return Result.ok(ebookService.list(new QueryWrapper(new Ebook(category2Id))));
    }

    /**
     * 进行数据分页的查询
     * @return
     */
    @GetMapping("/getPage")
    public Result getPageEbook(PageReq pageReq){
        //指定当前页和每页显示数据的条数
        Page page = new Page(pageReq.getCurrent(),pageReq.getSize());
        //进行数据的查询
        IPage<Ebook> ebookIPage = ebookService.page(page);
        return Result.ok(ebookIPage);
    }

    /**
     * 定义添加或者更新的接口
     */
    @PostMapping("/save")
    public Result save(@RequestBody Ebook ebook){
        Result reuslt =Result.ifSuccess(ebookService.saveOrUpdate(ebook));
        return reuslt;
    }
    /**
     * 定义删除接口
     */
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable long id){
        //1650314708473552898
        return Result.ifSuccess(ebookService.removeById(id));
    }

}
